package org.jeecg.modules.yfYqgk.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkZyry;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkTudcq;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkLzqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkGhqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkHqqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkXfaqqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkSdqrgyjxhqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgk;
import org.jeecg.modules.yfYqgk.vo.HqYfYqgkPage;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkZyryService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkTudcqService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkLzqkService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkGhqkService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkHqqkService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkXfaqqkService;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkSdqrgyjxhqkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 营区概况
 * @Author: jeecg-boot
 * @Date:   2020-07-12
 * @Version: V1.0
 */
@Api(tags="营区概况")
@RestController
@RequestMapping("/yfYqgk/hqYfYqgk")
@Slf4j
public class HqYfYqgkController {
	@Autowired
	private IHqYfYqgkService hqYfYqgkService;
	@Autowired
	private IHqYfYqgkZyryService hqYfYqgkZyryService;
	@Autowired
	private IHqYfYqgkTudcqService hqYfYqgkTudcqService;
	@Autowired
	private IHqYfYqgkLzqkService hqYfYqgkLzqkService;
	@Autowired
	private IHqYfYqgkGhqkService hqYfYqgkGhqkService;
	@Autowired
	private IHqYfYqgkHqqkService hqYfYqgkHqqkService;
	@Autowired
	private IHqYfYqgkXfaqqkService hqYfYqgkXfaqqkService;
	@Autowired
	private IHqYfYqgkSdqrgyjxhqkService hqYfYqgkSdqrgyjxhqkService;
	
	/**
	 * 分页列表查询
	 *
	 * @param hqYfYqgk
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "营区概况-分页列表查询")
	@ApiOperation(value="营区概况-分页列表查询", notes="营区概况-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HqYfYqgk hqYfYqgk,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HqYfYqgk> queryWrapper = QueryGenerator.initQueryWrapper(hqYfYqgk, req.getParameterMap());
		Page<HqYfYqgk> page = new Page<HqYfYqgk>(pageNo, pageSize);
		IPage<HqYfYqgk> pageList = hqYfYqgkService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param hqYfYqgkPage
	 * @return
	 */
	@AutoLog(value = "营区概况-添加")
	@ApiOperation(value="营区概况-添加", notes="营区概况-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HqYfYqgkPage hqYfYqgkPage) {
		HqYfYqgk hqYfYqgk = new HqYfYqgk();
		BeanUtils.copyProperties(hqYfYqgkPage, hqYfYqgk);
		hqYfYqgkService.saveMain(hqYfYqgk, hqYfYqgkPage.getHqYfYqgkZyryList(),hqYfYqgkPage.getHqYfYqgkTudcqList(),hqYfYqgkPage.getHqYfYqgkLzqkList(),hqYfYqgkPage.getHqYfYqgkGhqkList(),hqYfYqgkPage.getHqYfYqgkHqqkList(),hqYfYqgkPage.getHqYfYqgkXfaqqkList(),hqYfYqgkPage.getHqYfYqgkSdqrgyjxhqkList());
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param hqYfYqgkPage
	 * @return
	 */
	@AutoLog(value = "营区概况-编辑")
	@ApiOperation(value="营区概况-编辑", notes="营区概况-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HqYfYqgkPage hqYfYqgkPage) {
		HqYfYqgk hqYfYqgk = new HqYfYqgk();
		BeanUtils.copyProperties(hqYfYqgkPage, hqYfYqgk);
		HqYfYqgk hqYfYqgkEntity = hqYfYqgkService.getById(hqYfYqgk.getId());
		if(hqYfYqgkEntity==null) {
			return Result.error("未找到对应数据");
		}
		hqYfYqgkService.updateMain(hqYfYqgk, hqYfYqgkPage.getHqYfYqgkZyryList(),hqYfYqgkPage.getHqYfYqgkTudcqList(),hqYfYqgkPage.getHqYfYqgkLzqkList(),hqYfYqgkPage.getHqYfYqgkGhqkList(),hqYfYqgkPage.getHqYfYqgkHqqkList(),hqYfYqgkPage.getHqYfYqgkXfaqqkList(),hqYfYqgkPage.getHqYfYqgkSdqrgyjxhqkList());
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况-通过id删除")
	@ApiOperation(value="营区概况-通过id删除", notes="营区概况-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hqYfYqgkService.delMain(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "营区概况-批量删除")
	@ApiOperation(value="营区概况-批量删除", notes="营区概况-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hqYfYqgkService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况-通过id查询")
	@ApiOperation(value="营区概况-通过id查询", notes="营区概况-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HqYfYqgk hqYfYqgk = hqYfYqgkService.getById(id);
		if(hqYfYqgk==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(hqYfYqgk);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_住用人员通过主表ID查询")
	@ApiOperation(value="营区概况_住用人员主表ID查询", notes="营区概况_住用人员-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkZyryByMainId")
	public Result<?> queryHqYfYqgkZyryListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkZyry> hqYfYqgkZyryList = hqYfYqgkZyryService.selectByMainId(id);
		return Result.ok(hqYfYqgkZyryList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_土地产权通过主表ID查询")
	@ApiOperation(value="营区概况_土地产权主表ID查询", notes="营区概况_土地产权-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkTudcqByMainId")
	public Result<?> queryHqYfYqgkTudcqListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkTudcq> hqYfYqgkTudcqList = hqYfYqgkTudcqService.selectByMainId(id);
		return Result.ok(hqYfYqgkTudcqList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_领证情况通过主表ID查询")
	@ApiOperation(value="营区概况_领证情况主表ID查询", notes="营区概况_领证情况-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkLzqkByMainId")
	public Result<?> queryHqYfYqgkLzqkListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkLzqk> hqYfYqgkLzqkList = hqYfYqgkLzqkService.selectByMainId(id);
		return Result.ok(hqYfYqgkLzqkList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_规划情况通过主表ID查询")
	@ApiOperation(value="营区概况_规划情况主表ID查询", notes="营区概况_规划情况-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkGhqkByMainId")
	public Result<?> queryHqYfYqgkGhqkListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkGhqk> hqYfYqgkGhqkList = hqYfYqgkGhqkService.selectByMainId(id);
		return Result.ok(hqYfYqgkGhqkList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_划区情况通过主表ID查询")
	@ApiOperation(value="营区概况_划区情况主表ID查询", notes="营区概况_划区情况-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkHqqkByMainId")
	public Result<?> queryHqYfYqgkHqqkListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkHqqk> hqYfYqgkHqqkList = hqYfYqgkHqqkService.selectByMainId(id);
		return Result.ok(hqYfYqgkHqqkList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_消防安全情况通过主表ID查询")
	@ApiOperation(value="营区概况_消防安全情况主表ID查询", notes="营区概况_消防安全情况-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkXfaqqkByMainId")
	public Result<?> queryHqYfYqgkXfaqqkListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkXfaqqk> hqYfYqgkXfaqqkList = hqYfYqgkXfaqqkService.selectByMainId(id);
		return Result.ok(hqYfYqgkXfaqqkList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "营区概况_水、电、气、热供应及消耗情况通过主表ID查询")
	@ApiOperation(value="营区概况_水、电、气、热供应及消耗情况主表ID查询", notes="营区概况_水、电、气、热供应及消耗情况-通主表ID查询")
	@GetMapping(value = "/queryHqYfYqgkSdqrgyjxhqkByMainId")
	public Result<?> queryHqYfYqgkSdqrgyjxhqkListByMainId(@RequestParam(name="id",required=true) String id) {
		List<HqYfYqgkSdqrgyjxhqk> hqYfYqgkSdqrgyjxhqkList = hqYfYqgkSdqrgyjxhqkService.selectByMainId(id);
		return Result.ok(hqYfYqgkSdqrgyjxhqkList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hqYfYqgk
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HqYfYqgk hqYfYqgk) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<HqYfYqgk> queryWrapper = QueryGenerator.initQueryWrapper(hqYfYqgk, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<HqYfYqgk> queryList = hqYfYqgkService.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<HqYfYqgk> hqYfYqgkList = new ArrayList<HqYfYqgk>();
      if(oConvertUtils.isEmpty(selections)) {
          hqYfYqgkList = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          hqYfYqgkList = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<HqYfYqgkPage> pageList = new ArrayList<HqYfYqgkPage>();
      for (HqYfYqgk main : hqYfYqgkList) {
          HqYfYqgkPage vo = new HqYfYqgkPage();
          BeanUtils.copyProperties(main, vo);
          List<HqYfYqgkZyry> hqYfYqgkZyryList = hqYfYqgkZyryService.selectByMainId(main.getId());
          vo.setHqYfYqgkZyryList(hqYfYqgkZyryList);
          List<HqYfYqgkTudcq> hqYfYqgkTudcqList = hqYfYqgkTudcqService.selectByMainId(main.getId());
          vo.setHqYfYqgkTudcqList(hqYfYqgkTudcqList);
          List<HqYfYqgkLzqk> hqYfYqgkLzqkList = hqYfYqgkLzqkService.selectByMainId(main.getId());
          vo.setHqYfYqgkLzqkList(hqYfYqgkLzqkList);
          List<HqYfYqgkGhqk> hqYfYqgkGhqkList = hqYfYqgkGhqkService.selectByMainId(main.getId());
          vo.setHqYfYqgkGhqkList(hqYfYqgkGhqkList);
          List<HqYfYqgkHqqk> hqYfYqgkHqqkList = hqYfYqgkHqqkService.selectByMainId(main.getId());
          vo.setHqYfYqgkHqqkList(hqYfYqgkHqqkList);
          List<HqYfYqgkXfaqqk> hqYfYqgkXfaqqkList = hqYfYqgkXfaqqkService.selectByMainId(main.getId());
          vo.setHqYfYqgkXfaqqkList(hqYfYqgkXfaqqkList);
          List<HqYfYqgkSdqrgyjxhqk> hqYfYqgkSdqrgyjxhqkList = hqYfYqgkSdqrgyjxhqkService.selectByMainId(main.getId());
          vo.setHqYfYqgkSdqrgyjxhqkList(hqYfYqgkSdqrgyjxhqkList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "营区概况列表");
      mv.addObject(NormalExcelConstants.CLASS, HqYfYqgkPage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("营区概况数据", "导出人:"+sysUser.getRealname(), "营区概况"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<HqYfYqgkPage> list = ExcelImportUtil.importExcel(file.getInputStream(), HqYfYqgkPage.class, params);
              for (HqYfYqgkPage page : list) {
                  HqYfYqgk po = new HqYfYqgk();
                  BeanUtils.copyProperties(page, po);
                  hqYfYqgkService.saveMain(po, page.getHqYfYqgkZyryList(),page.getHqYfYqgkTudcqList(),page.getHqYfYqgkLzqkList(),page.getHqYfYqgkGhqkList(),page.getHqYfYqgkHqqkList(),page.getHqYfYqgkXfaqqkList(),page.getHqYfYqgkSdqrgyjxhqkList());
              }
              return Result.ok("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败！");
    }

}
